package work.zhengyun.demo.utils;

import org.apache.commons.io.FileUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author zhengyun
 * @date 2019/12/17 11:01
 */
public class JsoupHttpClient {
    public static void main(String[] args) throws ClientProtocolException, IOException {

        // 创建httpclient实例
        CloseableHttpClient httpclient = HttpClients.createDefault();
        // 创建httpget实例
        HttpGet httpget = new HttpGet("https://detail.tmall.com/item.htm?spm=a212k0.12153887.0.0.6df3687dGGo2xX&id=41727841498");

        // 模拟浏览器 ✔
        httpget.setHeader("User-Agent",
                "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0");

       /* // 使用代理 IP ✔
        HttpHost proxy = new HttpHost("39.137.69.8", 8080);
        RequestConfig config = RequestConfig.custom().setProxy(proxy)
                //设置连接超时 ✔
                .setConnectTimeout(10000) // 设置连接超时时间 10秒钟
                .setSocketTimeout(10000) // 设置读取超时时间10秒钟
                .build();

        httpget.setConfig(config);*/

        // 执行get请求
        CloseableHttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        // 获取返回实体
        String content = EntityUtils.toString(entity, "utf-8");
        String str=new String("");
        // 解析网页 得到文档对象
        Document doc = Jsoup.parse(str);
        // 获取指定的 <img />
        Elements elements = doc.getElementsByTag("img");
        //Elements elements = doc.select("#description img");
        System.out.println(elements.size());
        for (int i = 0; i < elements.size(); i++) {
            Element element = elements.get(i);
            // 获取 <img /> 的 src
            String url = element.attr("src");
            System.out.println("=="+url);
            // 再发请求最简单了，并由于该链接是没有 https:开头的，得人工补全 ✔
            if(url.startsWith("//img")){
                url="https:"+url;
            }
            if(url.startsWith("https://img")){

            }
            HttpGet PicturehttpGet = new HttpGet(url);
            try{
                CloseableHttpResponse pictureResponse = httpclient.execute(PicturehttpGet);
                HttpEntity pictureEntity = pictureResponse.getEntity();
                InputStream inputStream = pictureEntity.getContent();
                // 使用 common-io 下载图片到本地，注意图片名不能重复 ✔
                FileUtils.copyToFile(inputStream, new File("D://LLLLLLLLLLLLLLLLLLL//imagesTest//" + i + ".jpg"));
                pictureResponse.close(); // pictureResponse关闭
            }catch (Exception e){
                System.out.println(url);
                continue;
            }
        }

        response.close(); // response关闭
        httpclient.close(); // httpClient关闭

    }
}
